/*-
 * Copyright (c) 2009 Jonathan Anderson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef USER_ANGEL_H

#include <dirent.h>
#include <stdio.h>

#include <QList>
#include <QMap>
#include <QSharedPointer>
#include <QString>

struct ua_datum;


namespace userAngel
{
	class UserAngel
	{
		public:
		class Datum
		{
			public:
			/**
			 * Constructor.
			 *
			 * @param   d      the User Angel datum, which this class WILL take
			 *                 ownership of, i.e. free on destruction
			 */
			Datum(struct ua_datum* d, int fds[] = NULL, size_t fdcount = 0);

			int32_t toInt() const;
			int64_t toLong() const;
			QString toString() const;
			QByteArray toBytes() const;
			struct ua_powerbox_options toPowerbox() const;

			QList<int> fds() const;

			private:
			QSharedPointer<struct ua_datum> _datum;
			QList<int> _fds;
		};

		UserAngel(int socket);

		int find();
		void set(int socket);
		void ping() const;

		QString cacheFD(QString name, bool longLasting) const;
		int retrieve(QString token) const;

		QMap<QString,int> powerbox(struct ua_powerbox_options *options) const;

		int access(QString path, int mode);
		int stat(QString path, struct stat *s);
		int open(QString path, int flags, ...);
		int ropen(QString path, int flags, cap_rights_t rights, int umask);

		FILE* fopen(QString path, QString mode);
		DIR* opendir(QString filename);

		void send(int32_t, QList<int> fds = QList<int>()) const;
		void send(int64_t, QList<int> fds = QList<int>()) const;
		void send(QByteArray, QList<int> fds = QList<int>()) const;
		void send(QString, QList<int> fds = QList<int>()) const;
		void send(struct ua_powerbox_options*) const;
		void sendError(QString message, int errnum = 0) const;

		// low-level API
		Datum recv(size_t maxFDs = 0) const;

		int32_t unmarshall_int(const struct ua_datum *d) const;
		int64_t unmarshall_long(const struct ua_datum *d) const;
		QByteArray unmarshall_bytes(const struct ua_datum *d) const;
		QString unmarshall_string(const struct ua_datum *d) const;
		QString unmarshall_error(const struct ua_datum *d) const;
		struct ua_powerbox_options
			unmarshall_powerbox(const struct ua_datum *d) const;

		protected:
		void send(struct ua_datum *d, QList<int> fds = QList<int>()) const;

		struct ua_datum* marshall(int32_t) const;
		struct ua_datum* marshall(int64_t) const;
		struct ua_datum* marshall(QByteArray) const;
		struct ua_datum* marshall(QString) const;
		struct ua_datum* marshallError(QString message, int errnum) const;
		struct ua_datum* marshall(const struct ua_powerbox_options *options) const;

		private:
		int sock;           //!< connection to the other end
	};
}

#define USER_ANGEL_H
#endif

